# 编译说明：
# 该代码在infrastructure-om/infrastructure中编译使用，DT用例使用Framework/test工程。

# 1. cmake version，指定cmake版本
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
include(CMakePrintHelpers)

# 2. project name，指定项目的名称及全局编译选项
project(kmc_archive_test)

add_compile_options(
    -fPIC
    -O2
    -DWSEC_COMPILE_CAC_OPENSSL
    -DWSEC_AES_GCM_SUPPORT
    -DWSEC_USE_OPENSSL_110
    -DWSEC_COMPILE_MIP_OTHER
)

message(STATUS "CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "KMC include=${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/include/")

# 3. head file path，头文件目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/SecureCLib_rel/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/SecureCLib_rel/include/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/include/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/common/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/lib/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/sdp/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/cac/
    ${CMAKE_CURRENT_SOURCE_DIR}/../../third_open_src/openssl_rel/include
)

# 4. 编译libkmcv3（统一生成 SHARED 库）
aux_source_directory(kmcv3_src KMCV3_SRCS)
message(STATUS "KMCV3_SRCS: ${KMCV3_SRCS}")

add_library(kmcv3 SHARED ${KMCV3_SRCS})

# AIX 特殊处理（加链接选项 + 后缀）
if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
    message(STATUS "AIX platform detected: applying AIX-specific linker options")
    set_target_properties(kmcv3 PROPERTIES SUFFIX ".so")

    target_link_options(kmcv3 PRIVATE
        -Wl,-G
        -Wl,-bexpall
        -Wl,-bnoentry
        -Wl,-bbigtoc
    )
endif()

# 通用编译选项
target_compile_options(kmcv3
    PRIVATE
    -fPIC
    -fstack-protector-all
)

# 4.4 库文件路径配置
set(CMAKE_SKIP_RPATH TRUE)

target_link_directories(kmcv3
    PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/SecureCLib_rel/lib
    ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/KMCV3_rel/lib
    ${CMAKE_CURRENT_SOURCE_DIR}/../../third_open_src/openssl_rel/lib
)

target_include_directories(kmcv3
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/kmcv3_src
)

# 链接依赖库（不使用 -Wl,--whole-archive 之类）
target_link_libraries(kmcv3
    PRIVATE
    crypto securec KMC SDP dl c pthread
)

# 5. 设置输出目录
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
